package cn.parllay.dingdangproject.service.impl;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.parllay.dingdangproject.entity.Constants;
import cn.parllay.dingdangproject.entity.ReplyMsg;
import cn.parllay.dingdangproject.entity.ReplyServerBody;
import cn.parllay.dingdangproject.entity.RequestForm;
import cn.parllay.dingdangproject.entity.ResponseForm;
import cn.parllay.dingdangproject.service.IPushService;
import cn.parllay.dingdangproject.service.NettyChannelMap;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Service
public class PushServiceImpl implements IPushService {
	private static ObjectMapper jsonMapper = new ObjectMapper();
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm pushNotice(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param != null) {
			try {
				final Map<String, Object> paramMap = (Map<String, Object>) param.getData();
				
				final String storeNo=paramMap.get("storeNo")+"";
				if(StringUtils.hasText(storeNo) && !"null".equals(storeNo)) {
					//创建一个可缓存线程池
					ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
					cachedThreadPool.execute(new Runnable() {
						@Override
						public void run() {
							//给StoreNo的门店发送通知
							ReplyMsg replyMsg=new ReplyMsg();
					        ReplyServerBody replyBody;
							try {
								replyBody = new ReplyServerBody(jsonMapper.writeValueAsString(paramMap));
								replyMsg.setBody(replyBody);
								System.out.println("storeNo="+storeNo);
						        System.out.println("body="+jsonMapper.writeValueAsString(paramMap));
						        Set<String> set=NettyChannelMap.getKeys();
						        Iterator<String> it = set.iterator();
						        while (it.hasNext()) {
							          String str = it.next();
							          System.out.println("str----"+str);
							          if(StringUtils.hasText(str) && !"null".equals(str) && str.length()>8 ) {
							        	  if(storeNo.equals(str.substring(0,9))) {
							        		  NettyChannelMap.get(str).writeAndFlush(replyMsg);
							        		  System.out.println("-----------push notice success----------");
							        	  }
							          }
						        }
							} catch (JsonProcessingException e) {
								e.printStackTrace();
							}
					        
						}
						
					});
					
					/*//给StoreNo的门店发送通知
					Constants.setClientId(storeNo);
					ReplyMsg replyMsg=new ReplyMsg();
			        ReplyServerBody replyBody=new ReplyServerBody(jsonMapper.writeValueAsString(paramMap));
			        replyMsg.setBody(replyBody);
			        System.out.println(jsonMapper.writeValueAsString(paramMap));
			        Set<String> set=NettyChannelMap.getKeys();
			        Iterator<String> it = set.iterator();
			        while (it.hasNext()) {
				          String str = it.next();
				          if(StringUtils.hasText(str) && !"null".equals(str)) {
				        	  if(storeNo.equals(str.substring(0,8))) {
				        		  NettyChannelMap.get(str).writeAndFlush(replyMsg);
				        	  }
				          }
			        }*/
					
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
			return result;
		}
		return null;
	}
	public static void main(String[] args) {
		String str="123456789";
		System.out.println(str.substring(0,8));
	}

}


